﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using BinhSon.Dao;

namespace BinhSon.Services.Impl
{
    public partial class BinhSonServices
    {        
        public DataSet ExportListOfPhongThi(int hocKyId, int khoiId, int monHocId, int lanThiId, int heSo)
        {
            var dataSet = new DataSet();
            var currentYear = this._bsEntities.ConstantTable.Select(x => x.NamHocHienTai).SingleOrDefault();
            var mhchk =
                this._bsEntities.MonHocCuaHocKy.SingleOrDefault(
                    x =>
                        x.NamHocId == currentYear && x.HocKyId == hocKyId && x.KhoiId == khoiId &&
                        x.MonHocId == monHocId && x.LanThiId == lanThiId && x.HeSo == heSo);
            if (mhchk == null)
            {
                return dataSet;
            }
            //get ds phong thi
            var dsPhongThi = this._bsEntities.PhongThi.Where(x => x.MonHocCuaHocKyId == mhchk.MonHocCuaHocKyId);
            var count = 1;
            foreach (var phongThi in dsPhongThi)
            {
                var dataTable = new DataTable("Phòng " + count);
                dataSet.Tables.Add(dataTable);
                dataTable.Columns.Add("Lớp");
                dataTable.Columns.Add("Số thứ tự", typeof(int));
                dataTable.Columns.Add("Họ và chữ lót");
                dataTable.Columns.Add("Tên");
                dataTable.Columns.Add("Giới tính");
                dataTable.Columns.Add("Ngày sinh");
                dataTable.Columns.Add("Nơi sinh");
                
                //get ds hoc sinh cua phong thi
                var dsHocSinh =
                    this._bsEntities.PhongThiHocSinh.Where(x => x.PhongThiId == phongThi.PhongThiId).ToList();
                foreach (var phongThiHocSinh in dsHocSinh)
                {
                    var dataRow = dataTable.NewRow();
                    var gioiTinh = phongThiHocSinh.HocSinh.GioiTinh ? "Nam" : "Nữ";
                    dataRow[0] = phongThi.LopHoc.TenLop;
                    dataRow[1] = phongThiHocSinh.SoThuTu;
                    dataRow[2] = phongThiHocSinh.HocSinh.HoVaChuLot;
                    dataRow[3] = phongThiHocSinh.HocSinh.Ten;
                    dataRow[4] = gioiTinh;
                    dataRow[5] = phongThiHocSinh.HocSinh.NgaySinh.ToShortDateString();
                    dataRow[6] = phongThiHocSinh.HocSinh.NoiSinh;
                    dataTable.Rows.Add(dataRow);
                }
                count++;
            }
            return dataSet;
        }
    }
}
